package aqu.controlador.Contabilidad;
import aqu.persistencia.utilidad.*;

import aqu.controlador.LoginController;
import aqu.persistencia.Dao.Administracion.SegUsuarioManager;
import aqu.persistencia.Dao.Contabilidad.ConCuentaManager;
import aqu.persistencia.entity.ConCuenta;
import aqu.persistencia.entity.SegUsuario;
import com.icesoft.faces.component.tree.IceUserObject;
import java.util.Iterator;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@ManagedBean(name = "treeBeanCuenta")
@ViewScoped
public class TreeBeanCuenta {

    private DefaultTreeModel model;
    private UrlNodeUserObjectCuenta selectedNodeObject = null;
    private ConCuentaManager  ConCuentaManager  = new ConCuentaManager();
    private DefaultMutableTreeNode rootTreeNode;
    
    public TreeBeanCuenta() {


        ////////////////////////////////
        // create root node with its children expanded
        rootTreeNode = new DefaultMutableTreeNode();
        UrlNodeUserObjectCuenta rootObject = new UrlNodeUserObjectCuenta(rootTreeNode,this);
        rootObject.setText("Cuentas");
        rootObject.setUrl("");
        rootObject.setExpanded(true);
        rootTreeNode.setUserObject(rootObject);

        // model is accessed by by the ice:tree component
        model = new DefaultTreeModel(rootTreeNode);
        LlenarMenu("-", null);

    }

    public DefaultTreeModel getModel() {
        return model;
    }

    public void checkChanged(ValueChangeEvent event) {
        System.out.println("checkboxed selected " + event.getComponent().getId());
    }

    public void setSelectedNodeObject(UrlNodeUserObjectCuenta selectedNodeObject) {
//        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
//        CuentaController lc = (CuentaController) session.getAttribute("cuentaController");
        //usuario = lc.setCodigocuenta(selectedNodeObject.getUrl());
        Map<String,Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
        CuentaController lc =(CuentaController)viewMap.get("cuentaController");
        lc.setCodigocuenta(selectedNodeObject.getUrl());
        this.selectedNodeObject = selectedNodeObject;

    }

    public UrlNodeUserObjectCuenta getSelectedNodeObject() {
        return selectedNodeObject;
    }

    public String nodeC(ActionEvent event) {


        return "contentP.xhtml";
    }

    public String onTreeNodeClicked() {
        String a ="a";
        a="entro";
        return getSelectedNodeObject().getUrl();

    }

    public void LlenarMenu(String padre, DefaultMutableTreeNode TreeNode) {
        Iterator results = ConCuentaManager.buscarConSQLQuery("SELECT CON_DESCRIPCION_CUE,CON_CODIGO_CUE,CON_CODIGOPADRE_CUE,CON_CODIGO_NIVCUE FROM con_cuenta c where CON_CODIGOPADRE_CUE='"+padre+"' order by CON_CODIGO_CUE asc ");

        while (results.hasNext()) {
            Object[] row = (Object[]) results.next();
            System.out.println(row[0]);
            System.out.println(row[1]);
            System.out.println(row[2]);
            System.out.println(row[3]);
            

            DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
            UrlNodeUserObjectCuenta branchObject = new UrlNodeUserObjectCuenta(branchNode,this);
            branchObject.setText(row[0].toString());
            branchObject.setLeafIcon("/css/imagenes/tree/tree_document.gif");
            branchObject.setBranchContractedIcon("/css/imagenes/tree/tree_folder_close.gif");
            branchObject.setBranchExpandedIcon("/css/imagenes/tree/tree_folder_open.gif");
            branchObject.setUrl(row[1].toString());
            if (!row[3].toString().trim().equals("1")) {
                branchObject.setLeaf(true);
            }
            branchNode.setUserObject(branchObject);

            if (row[2].equals("-")) {
                rootTreeNode.add(branchNode);


            } else {
                //a_treeh.getChildren().add(treeh);
                TreeNode.add(branchNode);
            }
            LlenarMenu(row[1].toString(), branchNode);
        }


    }
}
